﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using YiYiTong.Rocket.EFDBFirst;
using YiYiTong.Rocket.Helper;
using YiYiTong.Rocket.Model;

namespace YiYiTong.Rocket.Business.Biz
{
    public class BusinessMsgBiz
    {
        #region 单例模式
        private BusinessMsgBiz()
        {
        }

        private static BusinessMsgBiz _instance;
        private static readonly object _syncObject = new object();

        public static BusinessMsgBiz Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (_syncObject)
                    {
                        if (_instance == null)
                        {
                            _instance = new BusinessMsgBiz();
                        }
                    }
                }
                return _instance;
            }
        }
        #endregion 单例模式

        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="msg"></param>
        public void SendBusinessMsg(ADZ_BusinessMsg msg)
        {
            try
            {
                ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
                msg.BusinessMsgName = EnumHelper.GetEnumItemDescription<BusinessMsgEnum>(msg.BusinessMsgType);
                msg.create_time = DateTime.Now;
                msg.is_send = 0;
                dbcontext.ADZ_BusinessMsg.Add(msg);
                dbcontext.SaveChanges();
            }
            catch (Exception ex)
            {
                LogHelper.Error($"发送业务消息失败：{ex.GetExcetionMessage()}");
            }
        }

        public void SendMobileMsg(string mobile_msg)
        {
            try
            {
                string create_notice_mobiles = ConfigHelper.AppSetting<string>("create_notice_mobile", "");
                if (!string.IsNullOrWhiteSpace(create_notice_mobiles))
                {
                    var arrs = Regex.Split(create_notice_mobiles, ",");
                    if (arrs.Length > 0)
                    {
                        foreach (var mob in arrs)
                        {
                            SmsBiz.Instance.SendMessage(mob, mobile_msg);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error($"发送短信失败：{ex.GetExcetionMessage()}");
            }
        }

        /// <summary>
        /// 为业务员配置业务消息
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="msg_type"></param>
        public void SetManagerMsgConfig(long uid, int msg_type)
        {
            try
            {
                ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
                ////增加此人的业务提醒消息
                var aDZ_BusinessMsg_UserConfig = dbcontext.ADZ_BusinessMsg_UserConfig.FirstOrDefault(t => t.UID == uid && t.BusinessMsgType == msg_type);
                if (aDZ_BusinessMsg_UserConfig == null)
                {
                    var defaultConfig = dbcontext.ADZ_BusinessMsg_UserConfig.FirstOrDefault(t => t.UID == 0 && t.BusinessMsgType == msg_type);
                    if (defaultConfig != null)
                    {
                        dbcontext.ADZ_BusinessMsg_UserConfig.Add(new ADZ_BusinessMsg_UserConfig
                        {
                            UID = uid,
                            BusinessMsgType = msg_type,
                            BusinessMsgName = defaultConfig.BusinessMsgName
                        });
                    }
                    dbcontext.SaveChanges();
                }
            }catch(Exception ex)
            {
                LogHelper.Error($"为业务员配置业务消息失败：{ex.GetExcetionMessage()}");
            }
        }
    }
}
